package com.ruoyi.function.controller;


import com.ruoyi.common.core.domain.AjaxResult;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.function.service.BusinessLicenseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api")
public class BusinessLicenseController {
    
    private static final Logger log = LoggerFactory.getLogger(BusinessLicenseController.class);

    @Autowired
    private BusinessLicenseService businessLicenseService;

    /**
     * 上传营业执照并验证
     */
    @PostMapping("/upload/business")
    public AjaxResult uploadBusinessLicense(@RequestParam("file") MultipartFile file) {
        try {
            // 检查文件
            if (file == null || file.isEmpty()) {
                return AjaxResult.error("请选择文件");
            }

            // 检查文件类型
            String contentType = file.getContentType();
            if (contentType == null || !contentType.startsWith("image/")) {
                return AjaxResult.error("请上传图片文件");
            }

            // 获取当前登录用户ID
            Long userId = SecurityUtils.getUserId();
            log.info("上传营业执照 - 用户ID: {}", userId);
    
            // 调用服务处理营业执照
            Object result = businessLicenseService.recognizeBusinessLicense(
                file.getInputStream(), 
                userId
            );

            return AjaxResult.success(result);

        } catch (Exception e) {
            log.error("营业执照处理失败", e);
            return AjaxResult.error("营业执照处理失败：" + e.getMessage());
        }
    }
}